<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="userPassword">
      <el-input v-model="dataForm.userPassword" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userHead">
      <el-input v-model="dataForm.userHead" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="userNickname">
      <el-input v-model="dataForm.userNickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="userGender">
      <el-input v-model="dataForm.userGender" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="地区id" prop="userAddressId">
      <el-input v-model="dataForm.userAddressId" placeholder="地区id"></el-input>
    </el-form-item>
    <el-form-item label="真实姓名" prop="userRealName">
      <el-input v-model="dataForm.userRealName" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="userBirthday">
      <el-input v-model="dataForm.userBirthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="userIdCard">
      <el-input v-model="dataForm.userIdCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="微博昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="微博昵称"></el-input>
    </el-form-item>
    <el-form-item label="微博uid" prop="socialUid">
      <el-input v-model="dataForm.socialUid" placeholder="微博uid"></el-input>
    </el-form-item>
    <el-form-item label="微博令牌" prop="accessToken">
      <el-input v-model="dataForm.accessToken" placeholder="微博令牌"></el-input>
    </el-form-item>
    <el-form-item label="微博登录过期时间" prop="expiresIn">
      <el-input v-model="dataForm.expiresIn" placeholder="微博登录过期时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          userPassword: '',
          userHead: '',
          userNickname: '',
          userGender: '',
          userAddressId: '',
          userRealName: '',
          userBirthday: '',
          userIdCard: '',
          nickname: '',
          socialUid: '',
          accessToken: '',
          expiresIn: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          userPassword: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          userHead: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          userNickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          userGender: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          userAddressId: [
            { required: true, message: '地区id不能为空', trigger: 'blur' }
          ],
          userRealName: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          userBirthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          userIdCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '微博昵称不能为空', trigger: 'blur' }
          ],
          socialUid: [
            { required: true, message: '微博uid不能为空', trigger: 'blur' }
          ],
          accessToken: [
            { required: true, message: '微博令牌不能为空', trigger: 'blur' }
          ],
          expiresIn: [
            { required: true, message: '微博登录过期时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.user.userName
                this.dataForm.userPassword = data.user.userPassword
                this.dataForm.userHead = data.user.userHead
                this.dataForm.userNickname = data.user.userNickname
                this.dataForm.userGender = data.user.userGender
                this.dataForm.userAddressId = data.user.userAddressId
                this.dataForm.userRealName = data.user.userRealName
                this.dataForm.userBirthday = data.user.userBirthday
                this.dataForm.userIdCard = data.user.userIdCard
                this.dataForm.nickname = data.user.nickname
                this.dataForm.socialUid = data.user.socialUid
                this.dataForm.accessToken = data.user.accessToken
                this.dataForm.expiresIn = data.user.expiresIn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/authserver/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'userPassword': this.dataForm.userPassword,
                'userHead': this.dataForm.userHead,
                'userNickname': this.dataForm.userNickname,
                'userGender': this.dataForm.userGender,
                'userAddressId': this.dataForm.userAddressId,
                'userRealName': this.dataForm.userRealName,
                'userBirthday': this.dataForm.userBirthday,
                'userIdCard': this.dataForm.userIdCard,
                'nickname': this.dataForm.nickname,
                'socialUid': this.dataForm.socialUid,
                'accessToken': this.dataForm.accessToken,
                'expiresIn': this.dataForm.expiresIn
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
